﻿'Project: Maaharaa ( Dhivehi Proofing Tool)
'Stand Alone Applicaton
'From main source
'Author: Ali Nashaath,Hassan Farish Rasheed. Mohamed Sujau

Imports System.Windows.Forms.RichTextBoxLanguageOptions
Imports NHunspell
Imports System.Drawing.Drawing2D
Imports System.Threading
Imports Maaharaa.Maaharaa

Public Class frmMain

    Dim spelltext As String() = Nothing
    Dim correct As Boolean = True
    Dim suggestions As List(Of String) 'list of suggestion for misspelled word
    Dim hunspell As New Hunspell("Dictionaries/dhivehi.aff", "Dictionaries/dhivehi.dic") 'spell checking engine
    Public errorwords As New List(Of String)
    Dim _path As String = ""
    Dim checkPrint As Integer = 0
    Dim myfont1 As New Font("MV Boli", 10)

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        'show the splash screen
        Me.Hide()
        Dim frmSplash As New splash
        frmSplash.Show()
        frmSplash.StartPosition = FormStartPosition.CenterScreen
        frmSplash.Update()
        Thread.Sleep(5000)
        frmSplash.Close()
        Me.Visible = True


        'applying current windows visual style
        Application.EnableVisualStyles()
        rtbInput.LanguageOption = AutoFontSizeAdjust

        'making sure the richtextbox does not have anythign at the startup
        rtbInput.Text = Nothing

        'setting the input language to Dhivehi
        For Each lang As InputLanguage In InputLanguage.InstalledInputLanguages
            If lang.Culture.DisplayName.ToUpper.Contains("DIVEHI") Then
                InputLanguage.CurrentInputLanguage = lang
                Exit For
            End If
        Next


    End Sub

#Region "Context Menu"

    Private Sub ContextmenuEdit_Opening(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles ContextmenuEdit.Opening

        Dim suggestions As List(Of String)
        suggestions = hunspell.Suggest(rtbInput.SelectedText)
        If errorwords.Contains(rtbInput.SelectedText.Replace(" ", "")) = True Then
            If suggestions.Count >= 1 Then
                If suggestions(0) <> Nothing Then
                    ToolStripSug1.Text = suggestions(0)
                End If
            End If
            If suggestions.Count >= 2 Then
                If suggestions(1) <> Nothing Then
                    ToolStripSug2.Text = suggestions(1)
                End If
            End If
            If suggestions.Count >= 3 Then
                If suggestions(2) <> Nothing Then
                    ToolstripSug3.Text = suggestions(2)
                End If
            End If
            If suggestions.Count >= 4 Then
                If suggestions(3) <> Nothing Then
                    ToolStripSug4.Text = suggestions(3)
                End If
            End If
            ToolStripSug1.Visible = True
            ToolStripSug2.Visible = True
            ToolstripSug3.Visible = True
            ToolStripSug4.Visible = True
        End If

        Dim dhivehithesaurus As New dvthesaurus(rtbInput.SelectedText.Replace(" ", ""))
        Dim thesuggestion As String() = dhivehithesaurus.getsuggestion

        For i As Integer = 0 To thesuggestion.Length - 1
            If i = 0 Then
                the1ToolStripMenuItem.Text = thesuggestion(i)
                the1ToolStripMenuItem.Visible = True
            End If

            If i = 1 Then
                the2ToolStripMenuItem.Text = thesuggestion(i)
                the2ToolStripMenuItem.Visible = True
            End If
            If i = 2 Then
                the3ToolStripMenuItem.Text = thesuggestion(i)
                the3ToolStripMenuItem.Visible = True
            End If
            If i = 3 Then
                the4ToolStripMenuItem.Text = thesuggestion(i)
                the4ToolStripMenuItem.Visible = True
            End If
            If i = 4 Then
                the5ToolStripMenuItem.Text = thesuggestion(i)
                the5ToolStripMenuItem.Visible = True
            End If
        Next



    End Sub

    Private Sub ContextmenuEdit_Closing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ToolStripDropDownClosingEventArgs) Handles ContextmenuEdit.Closing
        ToolStripSug1.Visible = False
        ToolStripSug2.Visible = False
        ToolstripSug3.Visible = False
        ToolStripSug4.Visible = False
        the1ToolStripMenuItem.Visible = False
        the2ToolStripMenuItem.Visible = False
        the3ToolStripMenuItem.Visible = False
        the4ToolStripMenuItem.Visible = False
        the5ToolStripMenuItem.Visible = False
    End Sub

    Private Sub CopyToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CopyToolStripMenuItem.Click
        rtbInput.Copy()
    End Sub

    Private Sub CutToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CutToolStripMenuItem.Click
        rtbInput.Cut()
    End Sub

    Private Sub PasteToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PasteToolStripMenuItem.Click
        rtbInput.Paste()
    End Sub

    Private Sub ToolStripSug1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripSug1.Click
        normalstate()
        removeErrorword(rtbInput.SelectedText.Replace(" ", ""))
        rtbInput.SelectedText = " " & ToolStripSug1.Text
    End Sub

    Private Sub ToolStripSug2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripSug2.Click
        normalstate()
        removeErrorword(rtbInput.SelectedText.Replace(" ", ""))
        rtbInput.SelectedText = " " & ToolStripSug2.Text
    End Sub

    Private Sub ToolstripSug3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolstripSug3.Click
        normalstate()
        removeErrorword(rtbInput.SelectedText.Replace(" ", ""))
        rtbInput.SelectedText = " " & ToolstripSug3.Text
    End Sub

    Private Sub ToolStripSug4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripSug4.Click
        normalstate()
        removeErrorword(rtbInput.SelectedText.Replace(" ", ""))
        rtbInput.SelectedText = " " & ToolStripSug4.Text
    End Sub

    Private Sub the1ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles the1ToolStripMenuItem.Click
        rtbInput.SelectedText = " " & the1ToolStripMenuItem.Text
    End Sub

    Private Sub the2ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles the2ToolStripMenuItem.Click
        rtbInput.SelectedText = " " & the2ToolStripMenuItem.Text
    End Sub

    Private Sub the3ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles the3ToolStripMenuItem.Click
        rtbInput.SelectedText = " " & the3ToolStripMenuItem.Text
    End Sub

    Private Sub the4ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles the4ToolStripMenuItem.Click
        rtbInput.SelectedText = " " & the4ToolStripMenuItem.Text
    End Sub

    Private Sub the5ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles the5ToolStripMenuItem.Click
        rtbInput.SelectedText = " " & the5ToolStripMenuItem.Text
    End Sub

#End Region

    Public Sub clearFormmating()
        Me.rtbInput.SelectAll()
        Me.rtbInput.SelectionFont = myfont1
        Me.rtbInput.SelectionColor = Color.Black
        Me.rtbInput.Select(0, 0)
    End Sub
    'spell checking
    Public Sub showErrors()
        'clearing the error word list
        errorwords.Clear()
        'getting words from the inputted text
        spelltext = rtbInput.Text.Replace(vbCrLf, " ").Split(CChar(" "))
        'checking all the words
        For Each item In spelltext
            correct = hunspell.Spell(item)
            'if a word is misspelled
            If correct = False Then
                'add that word to the error list
                errorwords.Add(item)
                'changing color of that word
                changecolor(item)
            End If
        Next
    End Sub

    'changing color of the misspelled words and underline those words
    Private Sub changecolor(ByVal text As String)
        Dim pos As Integer = 0
        Dim searchtext As String = text
        pos = rtbInput.Find(searchtext, pos, RichTextBoxFinds.MatchCase)
        Do While pos <> -1
            Dim myfont As New Font("MV Boli", 10, FontStyle.Underline)
            If rtbInput.SelectedText = searchtext Then
                rtbInput.SelectionLength = searchtext.Length
                rtbInput.SelectionColor = Color.Red
                rtbInput.SelectionUnderlineStyle = ExtendedRichTextBox.UnderlineStyle.HeavyWave
            End If
            pos = rtbInput.Find(searchtext, pos + 1, RichTextBoxFinds.MatchCase)
        Loop
    End Sub

    ' removing word from the error list

    Private Sub removeErrorword(ByVal word As String)
        'removing word from the error word list
        For Each word In errorwords
            If word.Contains(word) Then
                errorwords.Remove(word)
                'lblp.Text = errorwords.Count
                Exit For
            End If
        Next
    End Sub

    Private Sub normalstate()
        'changing back to the previous format 
        Dim myfont As New Font("MV Boli", 10)
        rtbInput.SelectionColor = Color.Black
        rtbInput.SelectionFont = myfont
    End Sub


    Private Sub rtbInput_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles rtbInput.MouseDown
        ' Getting the word under the mouse clicked loaction
        If e.Button = MouseButtons.Right Then
            Dim CharIndex As Int32
            Dim StartPos As Int32
            Dim EndPos As Int32

            'location of mouse clicked
            CharIndex = rtbInput.GetCharIndexFromPosition(New Point(e.X, e.Y))
            'Locating the end point of the selections
            EndPos = rtbInput.Text.IndexOf(" ", CharIndex)
            'Locating the start point of the selection
            StartPos = rtbInput.Text.LastIndexOf(" ", CharIndex)

            'Checking if the mouse clicked on word 
            ''If EndPos = -1 Then
            ''EndPos = 0
            ''End If
            If StartPos = -1 Then
                StartPos = 0
            End If
            If StartPos > EndPos Then
                Exit Sub
            End If
            'selecting the word under the clicked location
            CharIndex -= StartPos
            rtbInput.Select(StartPos, EndPos - StartPos)
        End If
    End Sub


    Private Sub rtbInput_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rtbInput.DoubleClick
        rtbInput.DeselectAll()
        Exit Sub
    End Sub


    Private Sub PictureBox2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles pctClose.Click
        Application.Exit()
    End Sub


    'wen form border ara customised... we need to move the form
    'by clicking anywhere outside of any control user can move the Main form
    'this part of the code is directly taken from the website below.
    'http://www.dreamincode.net/forums/topic/59643-moving-form-with-formborderstyle-none/

#Region "Form Move/Drag"

    Private IsFormBeingDragged As Boolean = False
    Private MouseDownX As Integer
    Private MouseDownY As Integer


    Private Sub pctBackground_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles pctBackGround.MouseDown
        If e.Button = MouseButtons.Left Then
            IsFormBeingDragged = True
            MouseDownX = e.X
            MouseDownY = e.Y
        End If
    End Sub

    Private Sub pctBackground_MouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles pctBackGround.MouseUp

        If e.Button = MouseButtons.Left Then
            IsFormBeingDragged = False
        End If
    End Sub

    Private Sub pctBackground_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles pctBackGround.MouseMove

        If IsFormBeingDragged Then
            Dim temp As Point = New Point()

            temp.X = Me.Location.X + (e.X - MouseDownX)
            temp.Y = Me.Location.Y + (e.Y - MouseDownY)
            Me.Location = temp
            temp = Nothing
        End If
    End Sub
#End Region

    Private Sub btnShowError_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnShowError.Click
        'calling the show error method to show the errors in the inputted text
        showErrors()
        'displaying the word count to the user
        'lbltotal.Text = spelltext.Count
        'displaying the number of errors to the user
        'lblp.Text = errorwords.Count
    End Sub

    Private Sub btnCheckSpell_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCheckSpell.Click
        rtbInput.Refresh()
        If rtbInput.Text = "" Then
            Exit Sub
        End If
        Dim myform As Form
        myform = New frmSpellcheck
        myform.Show()
    End Sub

    Private Sub btnOpenGrammar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOpenGrammar.Click
        If rtbInput.Text = "" Then
            Exit Sub
        End If
        Dim myform As Form
        myform = New frmGrammar
        myform.Show()
    End Sub


#Region "Main Menu items and Events"
#Region "Events"
    Private Sub UndoToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles UndoToolStripMenuItem.Click
        rtbInput.Undo()
    End Sub

    Private Sub RedoToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RedoToolStripMenuItem.Click
        rtbInput.Redo()
    End Sub

    Private Sub CutToolStripMenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CutToolStripMenuItem1.Click
        rtbInput.Cut()
    End Sub

    Private Sub CopyToolStripMenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CopyToolStripMenuItem1.Click
        rtbInput.Copy()
    End Sub

    Private Sub PasteToolStripMenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PasteToolStripMenuItem1.Click
        rtbInput.Paste()
    End Sub

    Private Sub SelectAllToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SelectAllToolStripMenuItem.Click
        rtbInput.SelectAll()
    End Sub

    Private Sub CustomizeToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CustomizeToolStripMenuItem.Click
        Dim myform As Form
        myform = New frmSpellcheck
        myform.Show()
    End Sub

    Private Sub OptionsToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OptionsToolStripMenuItem.Click
        Dim myform As Form
        myform = New frmGrammar
        myform.Show()
    End Sub

    Private Sub AboutToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AboutToolStripMenuItem.Click
        Dim myform As Form
        myform = New frmthesaurus
        myform.Show()
    End Sub

    Private Sub NewToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NewToolStripMenuItem.Click
        Clear()
    End Sub

    Private Sub OpenToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenToolStripMenuItem.Click
        Open()
    End Sub

    Private Sub SaveToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveToolStripMenuItem.Click
        Save(False)
    End Sub

    Private Sub SaveAsToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveAsToolStripMenuItem.Click
        Save(True)
    End Sub

    Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click
        Application.Exit()
    End Sub

#End Region
    
#Region "Methods"

    Private Sub Clear()
        _path = ""
        Me.rtbInput.Clear()
        Me.rtbInput.Select(0, 0)
        Me.spelltext = Nothing
        Me.correct = True

    End Sub

    Private Sub Open()
        Try
            Dim file As String = GetFilePath()

            If file <> "" Then
                Clear()
                Try
                    Me.rtbInput.Rtf = System.IO.File.ReadAllText(file, System.Text.Encoding.Default)
                Catch e1 As Exception 'error occured, that means we loaded invalid RTF, so load as plain text
                    Me.rtbInput.Text = System.IO.File.ReadAllText(file, System.Text.Encoding.Default)
                End Try
                _path = file
            End If
            file = Nothing
        Catch e2 As Exception
            Clear()
        End Try

    End Sub

    Private Sub Save(ByVal SaveAs As Boolean)
        Try
            If SaveAs = True Then
                Dim file As String = SetFilePath()

                If file <> "" Then
                    Me.rtbInput.SaveFile(file, RichTextBoxStreamType.RichText)
                    _path = file
                    file = Nothing
                End If
            Else
                If _path = "" Then
                    Dim file As String = SetFilePath()

                    If file <> "" Then
                        Me.rtbInput.SaveFile(file, RichTextBoxStreamType.RichText)
                        _path = file
                        file = Nothing
                    End If
                Else
                    Me.rtbInput.SaveFile(_path, RichTextBoxStreamType.RichText)
                End If
            End If
        Catch e1 As Exception
        End Try
    End Sub

    Private Function SetFilePath() As String
        Dim s As New SaveFileDialog()
        s.Filter = "RTF (*.rtf)|*.rtf|TXT (*.txt)|*.txt"
        If s.ShowDialog(Me) = DialogResult.OK Then
            Return s.FileName
        Else
            Return ""
        End If
    End Function

    Private Function GetFilePath() As String
        Dim o As New OpenFileDialog()
        o.Multiselect = False
        o.RestoreDirectory = True
        o.ShowReadOnly = False
        o.ReadOnlyChecked = False
        o.Filter = "RTF (*.rtf)|*.rtf|TXT (*.txt)|*.txt"
        If o.ShowDialog(Me) = DialogResult.OK Then
            Return o.FileName
        Else
            Return ""
        End If
    End Function
#End Region
    
#End Region

   
    
    Private Sub btnShowGrammar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnShowGrammar.Click
        Dim GrammarPatternList As Dictionary(Of String, String)
        Dim mygrammar As New Maaharaa.dvGrammar()
        GrammarPatternList = mygrammar.returnlist

        For Each item In GrammarPatternList
            Dim pos As Integer = 0
            Dim searchtext As String = item.Key
            pos = rtbInput.Find(searchtext, pos, RichTextBoxFinds.MatchCase)
            Do While pos <> -1
                Dim myfont As New Font("Faruma", 11, FontStyle.Bold)
                If rtbInput.SelectedText = searchtext Then
                    rtbInput.SelectionLength = searchtext.Length
                    rtbInput.SelectionColor = Color.Green
                    rtbInput.SelectionUnderlineStyle = ExtendedRichTextBox.UnderlineStyle.HeavyWave
                End If
                pos = rtbInput.Find(searchtext, pos + 1, RichTextBoxFinds.MatchCase)
            Loop
        Next
    End Sub

    Private Sub btnthesaurus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnthesaurus.Click
        Dim myform As Form
        myform = New frmthesaurus
        myform.Show()
    End Sub

    Private Sub އއގތބސToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles އއގތބސToolStripMenuItem.Click
        Dim myform As Form
        myform = New frmthesaurus
        myform.Show()
    End Sub
End Class

